package com.example.demo.config;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.util.HashMap;

@ControllerAdvice
@RestControllerAdvice//当前是针对controller的通知类（增强类）

public class MyExceptionAdvice {

    @ExceptionHandler(ArithmeticException.class)
    public HashMap<String,Object> arithmeticExceptionAdvice(ArithmeticException e){
        HashMap<String, Object> ret = new HashMap<>();
        ret.put("state", -1);
        ret.put("date", null);
        ret.put("msg", "算术异常 ：" + e.getMessage());
        return ret;
    }

    @ExceptionHandler(Exception.class)
    public HashMap<String,Object> exceptionAdvice(Exception e){
        HashMap<String, Object> ret = new HashMap<>();
        ret.put("state", -1);
        ret.put("date", null);
        ret.put("msg",  "异常 ：" + e.getMessage());
        return ret;
    }

}
